#include "PawnBase.h"

#include "PlayerStateBase.h"


APawnBase::APawnBase()
{
	PrimaryActorTick.bCanEverTick = true;
	AbilitySystemComponent = CreateDefaultSubobject<UBoukenAbilitySystemComponent>(FName("Ability System Component"));
	AbilitySystemComponent->SetIsReplicated(true);
	AbilitySystemComponent->SetReplicationMode(EGameplayEffectReplicationMode::Minimal);

	if (bSelfHoldAttributes)
	{
		Attributes = CreateDefaultSubobject<UBoukenAttributeSet>(TEXT("Attributes"));
	}
}

void APawnBase::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}

void APawnBase::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);
}

UAbilitySystemComponent* APawnBase::GetAbilitySystemComponent() const
{
	return AbilitySystemComponent;
}

void APawnBase::AcquireAbility(const TSubclassOf<UGameplayAbility> Ability, const int32 Level, const int32 InputID)
{
	if (!HasAuthority())
		return;
	auto ASC = GetAbilitySystemComponent();
	if (!ASC)
		return;
	ASC->GiveAbility(FGameplayAbilitySpec(Ability, Level, InputID));
	ASC->InitAbilityActorInfo(this, this);
}

void APawnBase::PossessedBy(AController* NewController)
{
	if (NewController)
	{
		const auto APlayerState = NewController->GetPlayerState<APlayerStateBase>();
		if (APlayerState)
		{
			AbilitySystemComponent = Cast<UBoukenAbilitySystemComponent>(APlayerState->GetAbilitySystemComponent());
			AbilitySystemComponent->InitAbilityActorInfo(APlayerState, this);
			Attributes = APlayerState->Attributes;
		}
	}
	Super::PossessedBy(NewController);
}
